<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Profile extends MY_Controller {
                
    public function __construct() {
        parent::__construct();
        $this->load->model("usraccount_model", "uam");
        $this->load->model("usrprofile_model", "upm");
        $this->load->model("usreducation_model", "edu");
        $this->load->model("usrjobshistory_model", "career");
        $this->load->model("usrprofilecontact_model", "contact");
        
        $this->picture_path = APPPATH.'../images/medium/';
    }

    function index() {
        $data["alumni"] = $this->upm->getById(@$this->sess->mpa_id_alumni);
        $data["pendidikan"] = $this->edu->getByUser(@$this->sess->mpa_id_alumni);
        $data["karir"] = $this->career->getByUser(@$this->sess->mpa_id_alumni);
        $data["contact"] = $this->contact->getByUser(@$this->sess->mpa_id_alumni);
        $this->myview($data, "profile/index");
    }

    function edit() {
        $data['datePickerCSS'] = "<link href=\"/assets/css/datepicker.css\" rel=\"stylesheet\">";
        $data['datePickerJS'] = "<script src=\"/assets/js/bootstrap-datepicker.js\"></script>";
        if ($this->input->post()) {
            $data["profile"] = new stdClass();
            $data["profile"]->up_fullname = $this->input->post("txtNama");
            $data["profile"]->up_display_name = $this->input->post("txtAlias");
            $data["profile"]->up_birth_place = $this->input->post("txtPob");
            $data["profile"]->up_birth_date = $this->input->post("txtDob");
            $data["profile"]->up_gender = $this->input->post("ddJenkel");
            $data["profile"]->up_graduate_year = $this->input->post("ddAngkatan");
            $data["profile"]->up_phone = $this->input->post("txtPhone");
            $data["profile"]->up_email = $this->input->post("txtEmail");
            $data["profile"]->up_address = $this->input->post("txtAlamatdomisili");
            $data["profile"]->city_id = $this->input->post("ddKotadomisili");
            $data["profile"]->province_id = $this->input->post("ddPropinsidomisili");
            $data["profile"]->up_zipcode = $this->input->post("txtZipdomisili");
            $data["profile"]->up_status = $this->input->post("ddStatus");
            $data["profile"]->up_home_phone = $this->input->post("txtTlpAsal");
            $data["profile"]->up_home_address = $this->input->post("txtAlamatasal");
            $data["profile"]->up_home_city_id = $this->input->post("ddKotaasal");
            $data["profile"]->up_home_province_id = $this->input->post("ddPropinsiasal");
            $data["profile"]->up_home_zipcode = $this->input->post("txtZipasal");
            $data["profile"]->up_father = $this->input->post("txtAyah");
            $data["profile"]->up_mother = $this->input->post("txtIbu");
            
            $data["account"] = new stdClass();
            $data["account"]->ua_username = $this->input->post("txtAlias");

            $data["alumni"] = new stdClass();
            foreach ($data["profile"] as $k => $v) {
                $data["alumni"]->$k = $v;
            }
            
            $data["alumni"]->up_fullname == "" ? $data["err"]->up_fullname = strErr("mustfilled") : "";
            $data["alumni"]->up_display_name == "" ? $data["err"]->up_display_name = strErr("mustfilled") : "";
            $data["alumni"]->up_birth_place == "" ? $data["err"]->up_birth_place = strErr("mustfilled") : "";
            $data["alumni"]->up_birth_date == "" ? $data["err"]->up_birth_date = strErr("mustfilled") : "";
            $data["alumni"]->up_gender == "" ? $data["err"]->up_gender = strErr("mustchoose") : "";
            $data["alumni"]->up_graduate_year == "" ? $data["err"]->up_graduate_year = strErr("mustchoose") : "";
            $data["alumni"]->up_phone == "" ? $data["err"]->up_phone = strErr("mustfilled") : "";
            !validEmail($data["alumni"]->up_email) ? $data["err"]->up_email = strErr("mustfilled") : "";
            $data["alumni"]->up_address == "" ? $data["err"]->up_address = strErr("mustfilled") : "";
            $data["alumni"]->city_id == "" ? $data["err"]->city_id = strErr("mustchoose") : "";
            $data["alumni"]->province_id == "" ? $data["err"]->province_id = strErr("mustchoose") : "";
            $data["alumni"]->up_zipcode == "" ? $data["err"]->up_zipcode = strErr("mustfilled") : "";
            $data["alumni"]->up_status == "" ? $data["err"]->up_status = strErr("mustchoose") : "";
            
            $data["alumni"]->up_home_phone == "" ? $data["err"]->up_home_phone = strErr("mustfilled") : "";
            $data["alumni"]->up_home_address == "" ? $data["err"]->up_home_address = strErr("mustfilled") : "";
            $data["alumni"]->up_home_city_id == "" ? $data["err"]->up_home_city_id = strErr("mustchoose") : "";
            $data["alumni"]->up_home_province_id == "" ? $data["err"]->up_home_province_id = strErr("mustchoose") : "";
            $data["alumni"]->up_home_zipcode == "" ? $data["err"]->up_home_zipcode = strErr("mustfilled") : "";
            $data["alumni"]->up_father == "" ? $data["err"]->up_father = strErr("mustfilled") : "";
            $data["alumni"]->up_mother == "" ? $data["err"]->up_mother = strErr("mustfilled") : "";
            
            if (@$data["err"]) {
                $this->myview(@$data, "profile/edit");
            } else {
                if ($this->upm->update($this->sess->mpa_id_alumni, $data["profile"]) == "" && $this->uam->update($this->sess->mpa_id_alumni, $data["account"]) == "") {
                    $this->session->set_flashdata("message", "Profile berhasil diubah");
                    $this->session->set_flashdata("redir", base_url() . "profile");
                    redirect(base_url() . "alert/success");
                } else {
                    $this->session->set_flashdata("message", "Profile gagal diubah");
                    $this->session->set_flashdata("redir", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
                    redirect(base_url() . "alert/error");
                }
            }
        } else {
            $data["alumni"] = $this->upm->getById($this->sess->mpa_id_alumni);
            $data["contact"] = $this->contact->getByUser(@$this->sess->mpa_id_alumni);
            $this->myview(@$data, "profile/edit", false);
        }
    }
    
    function contactAdd() {
        if ($this->input->post()) {
            $data["contact"]->upc_category = $this->input->post("txtCategory");
            $data["contact"]->upc_contact_label = $this->input->post("txtLabel");
            $data["contact"]->upc_contact_value = $this->input->post("txtName");
            $data["contact"]->upc_ispublic = $this->input->post("txtPublic");
            
            if ($data["contact"]->upc_category == "")
                $data["err"]->upc_category = strErr("mustchoose");
            if ($data["contact"]->upc_contact_label == "")
                $data["err"]->upc_contact_label = strErr("mustfilled");
            if ($data["contact"]->upc_contact_value == "")
                $data["err"]->upc_contact_value = strErr("mustfilled");
            
            if (@$data["err"]) {
                $this->myview(@$data, "profile/contact");
            } else {
                $id = $this->contact->insert(
                        $this->sess->mpa_id_alumni, 
                        $data["contact"]->upc_category, 
                        $data["contact"]->upc_contact_label, 
                        $data["contact"]->upc_contact_value,
                        $data["contact"]->upc_ispublic);
                $this->session->set_flashdata("message", "Contact berhasil disimpan");
                $this->session->set_flashdata("redir", base_url() . "profile");
                redirect(base_url() . "alert/success");
            }
            
        } else {
            $this->myview("", "profile/contact");
        }
    }
    
    function contactDel() {
        $this->contact->delete($this->uri->segment(3));
        $this->session->set_flashdata("message", "Contact berhasil dihapus");
        $this->session->set_flashdata("redir", base_url() . "profile");
        redirect(base_url() . "alert/success");
    }
     
    function educationAdd() {
        if ($this->input->post()) {
            $data["alumni"]->et_id = $this->input->post("eduTypeID");
            $data["alumni"]->ue_name = $this->input->post("eduName");
            $data["alumni"]->ue_major = $this->input->post("eduMajor");
            $data["alumni"]->ue_year_in = $this->input->post("eduYearIN");
            $data["alumni"]->ue_year_out = $this->input->post("eduYearOUT");
            $data["alumni"]->ue_description = $this->input->post("eduDescr") == '' ? '-' : $this->input->post("eduDescr");

            if ($data["alumni"]->et_id == "")
                $data["err"]->et_id = strErr("mustchoose");
            if ($data["alumni"]->ue_name == "")
                $data["err"]->ue_name = strErr("mustfilled");
            if ($data["alumni"]->ue_major == "")
                $data["err"]->ue_major = strErr("mustfilled");
            if ($data["alumni"]->ue_description == "")
                $data["err"]->educationdescr = strErr("mustfilled");

            if (@$data["err"]) {
                $this->myview(@$data, "profile/addEducation");
            } else {
                $id = $this->edu->insert($this->sess->mpa_id_alumni, $data["alumni"]->et_id, $data["alumni"]->ue_name, $data["alumni"]->ue_major, 0, $data["alumni"]->ue_year_in, $data["alumni"]->ue_year_out, $data["alumni"]->ue_description);
                $this->session->set_flashdata("message", "Pendidikan berhasil disimpan");
                $this->session->set_flashdata("redir", base_url() . "profile");
                redirect(base_url() . "alert/success");
            }
        } else {
            $this->myview("", "profile/addEducation");
        }
    }

    function educationEdit() {
        if ($this->input->post()) {
            $data["alumni"]->et_id = $this->input->post("eduTypeID");
            $data["alumni"]->ue_name = $this->input->post("eduName");
            $data["alumni"]->ue_major = $this->input->post("eduMajor");
            $data["alumni"]->ue_year_in = $this->input->post("eduYearIN");
            $data["alumni"]->ue_year_out = $this->input->post("eduYearOUT");
            $data["alumni"]->ue_description = $this->input->post("eduDescr") == '' ? '-' : $this->input->post("eduDescr");

            if ($data["alumni"]->et_id == "")
                $data["err"]->et_id = strErr("mustchoose");
            if ($data["alumni"]->ue_name == "")
                $data["err"]->ue_name = strErr("mustfilled");
            if ($data["alumni"]->ue_major == "")
                $data["err"]->ue_major = strErr("mustfilled");
            if ($data["alumni"]->ue_description == "")
                $data["err"]->educationdescr = strErr("mustfilled");

            if (@$data["err"]) {
                $this->myview(@$data, "profile/addEducation");
            } else {
                $id = $this->edu->update($this->uri->segment(3), $data["alumni"], array("mpa_id_alumni" => $this->sess->mpa_id_alumni));
                $this->session->set_flashdata("message", "Pendidikan berhasil diubah");
                $this->session->set_flashdata("redir", base_url() . "profile");
                redirect(base_url() . "alert/success");
            }
        } else {
            $dt["alumni"] = $this->edu->getById($this->uri->segment(3), array("mpa_id_alumni" => $this->sess->mpa_id_alumni));
            $this->myview($dt, "profile/addEducation");
        }
    }

    function careerAdd() {
        if ($this->input->post()) {
            $data["alumni"]->company_id = $this->input->post("txtOfficeid");
            $data["alumni"]->ujh_position = $this->input->post("position");
            $data["alumni"]->ujh_year_in = $this->input->post("yearin");
            $data["alumni"]->ujh_year_out = $this->input->post("yearout");

            if ($data["alumni"]->company_id == "")
                $data["err"]->companyid = strErr("mustchoose");
            if ($data["alumni"]->ujh_position == "")
                $data["err"]->position = strErr("mustfilled");
            if ($data["alumni"]->ujh_year_in == "")
                $data["err"]->yearin = strErr("mustfilled");
            if ($data["alumni"]->ujh_year_out == "")
                $data["err"]->yearout = strErr("mustfilled");

            if (@$data["err"]) {
                $this->myview(@$data, "profile/addCareer");
            } else {
                $id = $this->career->insert($this->sess->mpa_id_alumni, $data["alumni"]->company_id, $data["alumni"]->ujh_position, $data["alumni"]->ujh_year_in, $data["alumni"]->ujh_year_out);
                $this->session->set_flashdata("message", "Karir berhasil disimpan");
                $this->session->set_flashdata("redir", base_url() . "profile");
                redirect(base_url() . "alert/success");
            }
        } else {
            $this->myview("", "profile/addCareer");
        }
    }

    function careerEdit() {
        if ($this->input->post()) {
            $data["alumni"]->company_id = $this->input->post("txtOfficeid");
            $data["alumni"]->ujh_position = $this->input->post("position");
            $data["alumni"]->ujh_year_in = $this->input->post("yearin");
            $data["alumni"]->ujh_year_out = $this->input->post("yearout");

            if ($data["alumni"]->company_id == "")
                $data["err"]->companyid = strErr("mustchoose");
            if ($data["alumni"]->ujh_position == "")
                $data["err"]->position = strErr("mustfilled");
            if ($data["alumni"]->ujh_year_in == "")
                $data["err"]->yearin = strErr("mustfilled");
            if ($data["alumni"]->ujh_year_out == "")
                $data["err"]->yearout = strErr("mustfilled");

            if (@$data["err"]) {
                $this->myview(@$data, "profile/addCareer");
            } else {
                $id = $this->career->update($this->uri->segment(3), $data["alumni"], array("mpa_id_alumni" => $this->sess->mpa_id_alumni));
                $this->session->set_flashdata("message", "Karir berhasil diubah");
                $this->session->set_flashdata("redir", base_url() . "profile");
                redirect(base_url() . "alert/success");
            }
        } else {
            $dt["alumni"] = $id = $this->career->getById($this->uri->segment(3), array("mpa_id_alumni" => $this->sess->mpa_id_alumni));
            $this->myview($dt, "profile/addCareer");
        }
    }

    function work() {
        if ($this->input->post()) {
            $data["alumni"]->kantorid = $this->input->post("txtKantorid");
            $data["alumni"]->bagian = $this->input->post("txtBagian");
            $data["alumni"]->jabatan = $this->input->post("txtJabatan");
            $data["alumni"]->daritahun = $this->input->post("txtDari");
            $data["alumni"]->sampaitahun = $this->input->post("txtSampai");

            if ($data["alumni"]->kantorid == "")
                $data["err"]->kantorid = strErr("mustchoose");
            if ($data["alumni"]->jabatan == "")
                $data["err"]->jabatan = strErr("mustfilled");
            if ($data["alumni"]->bagian == "")
                $data["err"]->bagian = strErr("mustfilled");

            if (@$data["err"]) {
                $data["alumni"]->kantornama = $this->input->post("txtKantornama");
                $this->myview(@$data, "profile/work");
            } else {
                $id = $this->km->insert($this->sess->alumniid, $data["alumni"]->kantorid, $data["alumni"]->bagian, $data["alumni"]->jabatan, $data["alumni"]->daritahun, $data["alumni"]->sampaitahun);
                $this->session->set_flashdata("message", "Pekerjaan berhasil disimpan");
                $this->session->set_flashdata("redir", base_url() . "profile");
                redirect(base_url() . "alert/success");
            }
        } else {
            $this->myview("", "profile/work");
        }
    }

    function workedit() {
        if ($this->input->post()) {
            $data["alumni"]->kantorid = $this->input->post("txtKantorid");
            $data["alumni"]->bagian = $this->input->post("txtBagian");
            $data["alumni"]->jabatan = $this->input->post("txtJabatan");
            $data["alumni"]->daritahun = $this->input->post("txtDari");
            $data["alumni"]->sampaitahun = $this->input->post("txtSampai");

            if ($data["alumni"]->kantorid == "")
                $data["err"]->kantorid = strErr("mustchoose");
            if ($data["alumni"]->jabatan == "")
                $data["err"]->jabatan = strErr("mustfilled");
            if ($data["alumni"]->bagian == "")
                $data["err"]->bagian = strErr("mustfilled");

            if (@$data["err"]) {
                pre($data["err"]);
                $data["alumni"]->kantornama = $this->input->post("txtKantornama");
                $this->myview(@$data, "profile/work");
            } else {
                $id = $this->km->update($this->uri->segment(3), $data["alumni"], array("alumniid" => $this->sess->alumniid));
                $this->session->set_flashdata("message", "Pekerjaan berhasil diubah");
                $this->session->set_flashdata("redir", base_url() . "profile");
                redirect(base_url() . "alert/success");
            }
        } else {
            $data["alumni"] = $this->km->getById($this->uri->segment(3));
            $this->myview($data, "profile/work");
        }
    }

    function addkerabat() {
        if ($this->input->post()) {
            $data["alumni"]->alumninama = $this->input->post("txtNama");
            $data["alumni"]->alumnialamatdomisili = $this->input->post("txtAlamatdomisili");
            $data["alumni"]->alumnikotadomisili = $this->input->post("txtKotadomisili");
            $data["alumni"]->alumnipropinsidomisili = $this->input->post("txtPropinsidomisili");
            $data["alumni"]->alumnicontact = $this->input->post("txtContact");
            $data["alumni"]->alumniemail = $this->input->post("txtEmail");
            $data["alumni"]->alumnijenkel = $this->input->post("ddJenkel");
            $data["alumni"]->alumnistatus = $this->input->post("txtStatus");
            $data["alumni"]->alumnitype = $this->input->post("ddHubungan");

            $data["alumni"]->alumninama == "" ? $data["err"]->alumninama = strErr("mustfilled") : "";
            $data["alumni"]->alumnialamatdomisili == "" ? $data["err"]->alumnialamatdomisili = strErr("mustfilled") : "";
            $data["alumni"]->alumnikotadomisili == "" ? $data["err"]->alumnikotadomisili = strErr("mustchoose") : "";
            $data["alumni"]->alumnipropinsidomisili == "" ? $data["err"]->alumnipropinsidomisili = strErr("mustchoose") : "";
            $data["alumni"]->alumnicontact == "" ? $data["err"]->alumnicontact = strErr("mustfilled") : "";
            $data["alumni"]->alumniemail == "" ? $data["err"]->alumniemail = strErr("mustfilled") : "";
            !validEmail($data["alumni"]->alumniemail) ? $data["err"]->alumniemail = strErr("mustfilled") : "";
            $data["alumni"]->alumnistatus == "" ? $data["err"]->alumnistatus = strErr("mustfilled") : "";

            if (@$data["err"]) {
                $this->myview(@$data, "profile/edit");
            } else {
                if ($this->am->insert($data["alumni"]->alumninama, "-", "", "", "", $data["alumni"]->alumnialamatdomisili, $data["alumni"]->alumnikotadomisili, $data["alumni"]->alumnipropinsidomisili, $data["alumni"]->alumnicontact, $data["alumni"]->alumniemail, "sekretariatpusat", "Y", $data["alumni"]->alumnijenkel, $data["alumni"]->alumnistatus, $data["alumni"]->alumnitype, $this->sess->alumniid, "", "", $this->sess->alumniid, $this->sess->alumniid)) {
                    $this->session->set_flashdata("message", "Kerabat berhasil disimpan");
                    $this->session->set_flashdata("redir", base_url() . "profile");
                    redirect(base_url() . "alert/success");
                } else {
                    l_query();
                    $this->session->set_flashdata("message", "Kerabat gagal didisimpan");
                    $this->session->set_flashdata("redir", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
                    redirect(base_url() . "alert/error");
                }
            }
        } else {
            $this->myview("", "profile/addkerabat");
        }
    }

    function newpassword() {
        if ($this->input->post()) {
            $checkPassword = $this->uam->getById($this->sess->mpa_id_alumni);
            if ($checkPassword->ua_password == md5($this->input->post("password"))) {
                if (@$data["err"]) {
                    $this->myview(@$data, "profile/newpassword");
                } else {
                    if ($this->uam->update($this->sess->mpa_id_alumni, array('ua_password' => md5($this->input->post("newpassword"))))) {
                        $this->session->set_flashdata("message", "Password berhasil diubah");
                        $this->session->set_flashdata("redir", base_url() . "profile");
                        redirect(base_url() . "alert/success");
                    } else {
                        $this->session->set_flashdata("message", "Password gagal diubah");
                        $this->session->set_flashdata("redir", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
                        redirect(base_url() . "alert/error");
                    }
                }
            }else{
                $data["err"]->password = "Password yang anda masukkan salah";
                $this->myview(@$data, "profile/newpassword");
            }
        } else {
            $this->myview(@$data, "profile/newpassword", false);
        }
    }
    
    function picture() {
        $mpa_id_alumni = $this->uri->segment(3);
        if ($mpa_id_alumni == '') {
            $this->myview("", "profile/picture");
        } else {
            $this->load->helper('file');
            
            $alumni = $this->upm->getById($mpa_id_alumni);
            $ext = end(explode('.', $alumni['up_picture']));
            $image_content = read_file($this->picture_path . $alumni['up_picture']);
            header('Content-Length: ' . strlen($image_content));
            header('Content-Type: image/'.$ext);
            exit($image_content);
        }
    }
    
    function upload() {
        $dest_file_path = $this->picture_path;
        log_message('debug', $dest_file_path);
                
        $config['upload_path'] = $dest_file_path;
        $config['allowed_types'] = 'jpg|png|gif|jpeg';
        $config['max_size']	= '200';
        //$config['max_width']  = '1024';
        //$config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload()){
            $data['upload'] = array('error' => $this->upload->display_errors());
            $this->myview(@$data, "profile/picture");
        } else {   
            $data['upload'] = $this->upload->data();
            $dest_file_name = $this->session->userdata('mpa_id_alumni').$data['upload']['file_ext'];
            if (file_exists($dest_file_path.$dest_file_name)) {
                unlink($dest_file_path.$dest_file_name);
            }
            
            rename($data['upload']['full_path'], $dest_file_path.$dest_file_name);
            
            $this->upm->updatePicture($this->session->userdata('mpa_id_alumni'), $dest_file_name);
            
            $this->session->set_flashdata("message", "Profile picture berhasil diupload.");
            $this->session->set_flashdata("redir", base_url() . "profile");
            redirect(base_url() . "alert/success");
        }
    }

}

?>
